#include "subs.h"

/*
Direction Type Address       Text              
--------- ---- -------       ----              
Up        p    sub_257200+D  call    sub_28B9B0
Up        p    sub_26DC40+A0 call    sub_28B9B0
Up        p    sub_26EBA0+27 call    sub_28B9B0
*/

//#define LOAD_AT_TMP_FOLDER

UINT sub0003B9B0 ( LPCWSTR lpPrefixString, UINT uUnique, LPWSTR lpTempFileName )
{
	/* TODO:
		Detour Those
	*/
	WCHAR*(*sub_681A4) (int a1, int a2) = (WCHAR*(__cdecl*) (int, int))(__BaseAddress + 0x681A4);
	void(*sub_67546) (LPVOID lpMem) = (void(__cdecl*) (LPVOID))(__BaseAddress + 0x67546);

	log ( "[sub0003B9B0] Detour." );
	
	WCHAR* l_PathName = NULL; // eax@1
	UINT l_Return; // eax@2

	l_PathName = ( WCHAR* )sub_681A4 ( 260, 2 ); // Allocate Buffer
	if ( l_PathName )
	{
		char l_TempFileName[ MAX_PATH ] = { 0 };

		#ifdef LOAD_AT_TMP_FOLDER
		
			GetTempPathW ( MAX_PATH, l_PathName );
			l_Return = GetTempFileNameW ( l_PathName, lpPrefixString, uUnique, lpTempFileName );

			wcstombs ( l_TempFileName, lpTempFileName, MAX_PATH );

			log ( "[sub0003B9B0] TempFileName %s", l_TempFileName );

		#else

			char l_ApplicationPath[ MAX_PATH ] = { 0x00 };
			::GetModuleFileName ( 0, l_ApplicationPath, MAX_PATH );

			char l_ApplicationDrive[ MAX_PATH ] = { 0 };
			char l_ApplicationDirectory[ MAX_PATH ] = { 0 };
			_splitpath ( l_ApplicationPath, l_ApplicationDrive, l_ApplicationDirectory, NULL, NULL );

			sprintf ( l_TempFileName, "%s%sLibrary", l_ApplicationDrive, l_ApplicationDirectory );

			int l_Size = MultiByteToWideChar ( CP_ACP, 0, l_TempFileName, -1, NULL, 0 );
			MultiByteToWideChar ( CP_ACP, 0, l_TempFileName, -1, lpTempFileName, l_Size );

			log ( "[sub0003B9B0] TempFileName %s", l_TempFileName );

		#endif

		sub_67546 ( ( LPVOID )l_PathName ); // Free ( l_PathName )??
	}
	else
	{
		l_Return = 0;
	}
	return l_Return;
}
